package main

import (
	"context"
	"fmt"
	"github.com/mark3labs/mcp-go/client"
	"github.com/mark3labs/mcp-go/mcp"
	"os"
	"time"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Println("请提供 MCP 服务器地址作为参数。")
		return
	}
	serverAddr := os.Args[1]

	// 创建 MCP 客户端
	mcpClient, err := client.NewStdioMCPClient(serverAddr, []string{})
	if err != nil {
		panic(err)
	}
	defer mcpClient.Close()

	// 初始化客户端连接
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	initRequest := mcp.InitializeRequest{}
	initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
	initRequest.Params.ClientInfo = mcp.Implementation{
		Name:    "Client Demo",
		Version: "1.0.0",
	}
	initResult, err := mcpClient.Initialize(ctx, initRequest)
	if err != nil {
		panic(err)
	}
	fmt.Printf("初始化成功，服务器信息: %s %s\n", initResult.ServerInfo.Name, initResult.ServerInfo.Version)

	// 调用远程工具
	toolRequest := mcp.CallToolRequest{
		Request: mcp.Request{
			Method: "tools/call",
		},
	}
	toolRequest.Params.Name = "calculate"
	toolRequest.Params.Arguments = map[string]interface{}{
		"operation": "add",
		"x":         10,
		"y":         20,
	}

	result, err := mcpClient.CallTool(ctx, toolRequest)
	if err != nil {
		fmt.Printf("调用工具出错: %v\n", err)
		return
	}
	fmt.Printf("工具调用结果: %v\n", result)
}
